<template>
  <div class="app-container table-app-container">
    <div class="fixed-table">
      <div class="fixed-table-actionbar">
        <TableFilter />
        <ActionBar v-role="['workerGroupLeader', 'zongjian']" />
      </div>
      <PageableTable
        :loading="loading"
        :data="insideTableData"
        :total="totalElements"
        :page="currentPage"
        :page-size="pageSize"
        :table-height="tableHeight"
        class="fixed-table-table"
        :multiple-selected="false"
        @page-change="onPageChange"
        @sort-change="onSortChange"
      >
        <el-table-column v-for="column in myColumns" :key="column.key" :label="column.title" :prop="column.key" :sortable="column.sortable">
          <template slot-scope="scope">
            <column-render :render="column.render" :row="scope.row" :column="column" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button v-if="!row.published" v-role="['workerGroupLeader','zongjian']" type="text" title="编辑" size="mini" icon="el-icon-edit" @click="edit(row.id)" />
            <el-button v-if="!row.published" v-role="['workerGroupLeader','zongjian']" type="text" title="发布" size="mini" icon="el-icon-s-promotion" @click="onPublish(row.id)" />
            <el-button v-if="!row.published" v-role="['workerGroupLeader','zongjian']" type="text" title="删除" size="mini" icon="el-icon-delete" @click="remove(row.id)" />
            <el-button type="text" title="查看统计" size="mini" icon="el-icon-s-data" @click="view(row)" />
          </template>
        </el-table-column>
      </PageableTable>
    </div>
    <FormDialog title="编辑" :show.sync="dialogFormVisible" width="50%" @on-ok="ok" @on-cancel="cancel">
      <EditForm :id="editId" ref="editForm" />
    </FormDialog>
    <ViewDialog title="统计" class="fixed-dialog-height" :show.sync="dialogViewVisible" width="50%" @on-cancel="viewCancel">
      <ViewForm v-if="dialogViewVisible" :id="editId" :task="task" />
    </ViewDialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
// secondary package based on el-pagination
import FormDialog from '@neu/biz-core/components/FormDialog'
import ViewDialog from '@neu/biz-core/components/ViewDialog'
// 表格查询组件独立编写，需要传递pageable-table实例的mixin方法filter
import TableFilter from './components/tableFilter'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './components/editForm'
import ViewForm from './components/viewForm'
import ActionBar from './components/actionBar'
import PageableTable from '@neu/biz-core/components/PageableTable'
import databind from '@neu/biz-core/mixins/databind'
import role from '@neu/biz-core/directive/role/index.js' // 权限判断指令
import {
  page,
  remove,
  publish
} from '@neu/dcom-ui/api/questionnaire'
import { list as operationGroupList } from '@neu/dcom-ui/api/operationGroup'

const types = {
  'MIDDLE_YEAR_SUMMARY': '年中总结',
  'END_YEAR_SUMMARY': '年末总结'
}

export default {
  name: 'QuestionnairePage',
  components: {
    TableFilter,
    FormDialog,
    ViewDialog,
    EditForm,
    ViewForm,
    ActionBar,
    PageableTable
  },
  directives: { role },
  // 加入混入，使表格混入更多的api操作
  mixins: [databind],
  data() {
    return {
      r: page,
      d: remove,
      insideColumns: [
        {
          title: '任务标题',
          key: 'title',
          sortable: 'custom'
        },
        {
          title: '运维小组',
          key: 'groupCode',
          render: (h, params) => {
            return (
              <span>{ this.getOperationGroupName(params.row.groupCode) }</span>
            )
          }
        },
        {
          title: '所属年',
          key: 'year'
        },
        {
          title: '类型',
          key: 'type',
          render: (h, params) => { return h('span', types[params.row.type]) }
        },
        {
          title: '状态',
          key: 'published',
          render: (h, params) => {
            return (
              <el-tag type={params.row.published ? 'success' : 'info'}>{ params.row.published ? '已发布' : '未发布'}</el-tag>
            )
          }
        },
        {
          title: '发布日期',
          key: 'publishAt'
        },
        {
          title: '截止日期',
          key: 'closedAt'
        }
      ],
      // 编辑新增弹窗
      dialogFormVisible: false,
      dialogViewVisible: false,
      editId: -1,
      availableOperationGroups: []
    }
  },
  computed: {
    ...mapGetters([
      'roles'
    ]),
    isWorkerGroupLeader: function() {
      return this.roles.some(r => r === 'workerGroupLeader')
    },
    myColumns: function() {
      return this.isWorkerGroupLeader ? this.insideColumns : this.insideColumns.filter(it => it.key !== 'groupCode')
    }
  },
  created() {
    operationGroupList().then(response => {
      this.availableOperationGroups = response.data
    })
    this.filter({ s_scope: this.isWorkerGroupLeader ? 'WORKER' : 'LEADER' })
  },
  methods: {
    view(row) {
      // 打开对话框
      this.editId = row.id
      this.task = row
      this.dialogViewVisible = true
    },
    // 表格编辑按钮点击事件处理
    edit(id) {
      // 打开对话框
      this.editId = id
      this.dialogFormVisible = true
      // 更新当前被编辑实体的ID，自动传递至编辑表单
    },
    // 编辑资产对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 关闭对话框
        this.editId = -1
        this.dialogFormVisible = false
      })
    },
    cancel() {
      this.editId = -1
      // this.$refs.editForm.reset()
      // 关闭对话框
      this.dialogFormVisible = false
    },
    viewCancel() {
      this.editId = -1
      // 关闭对话框
      this.dialogViewVisible = false
    },
    onPublish(id) {
      publish(id).then(response => {
        this.$message({
          showClose: true,
          message: '发布成功！',
          type: 'success'
        })
        this.filter({ s_scope: this.isWorkerGroupLeader ? 'WORKER' : 'LEADER' })
      })
    },
    getOperationGroupName(code) {
      const group = this.availableOperationGroups.find(it => it.code === code)
      return group ? group.name : code
    }
  }
}
</script>
